package com.beimi.web.handler;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.beimi.core.BMDataContext;
import com.beimi.dao.model.User;
import com.beimi.util.UKTools;

@Controller
@SessionAttributes
public class Handler {
	public final static int PAGE_SIZE_BG = 1;
	public final static int PAGE_SIZE_TW = 20;
	public final static int PAGE_SIZE_FV = 50;
	public final static int PAGE_SIZE_HA = 100;

	public User getUser(HttpServletRequest request) {
		User user = (User) request.getSession(true).getAttribute(BMDataContext.USER_SESSION_NAME);
		if (user == null) {
			user = new User();
			user.setId(UKTools.getContextID(request.getSession().getId()));
			user.setUsername(BMDataContext.GUEST_USER + "_" + user.getId());
			user.setOrgi(BMDataContext.SYSTEM_ORGI);
			user.setSessionid(user.getId());
		} else {
			user.setSessionid(user.getId());
		}
		return user;
	}

	public User getIMUser(HttpServletRequest request, String sign) {
		User user = (User) request.getSession(true).getAttribute(BMDataContext.IM_USER_SESSION_NAME);
		if (user == null) {
			user = new User();
			if (!StringUtils.isBlank(sign)) {
				user.setId(sign);
			} else {
				user.setId(UKTools.getContextID(request.getSession().getId()));
			}
			user.setUsername(BMDataContext.GUEST_USER + "_" + user.getId());
			user.setSessionid(user.getId());
		} else {
			user.setSessionid(UKTools.getContextID(request.getSession().getId()));
		}
		return user;
	}

	public void setUser(HttpServletRequest request, User user) {
		request.getSession(true).removeAttribute(BMDataContext.USER_SESSION_NAME);
		request.getSession(true).setAttribute(BMDataContext.USER_SESSION_NAME, user);
	}

	public int getP(HttpServletRequest request) {
		int page = 0;
		String p = request.getParameter("p");
		if (!StringUtils.isBlank(p) && p.matches("[\\d]*")) {
			page = Integer.parseInt(p);
			if (page > 0) {
				page = page - 1;
			}
		}
		return page;
	}

	public int getPs(HttpServletRequest request) {
		int pagesize = PAGE_SIZE_TW;
		String ps = request.getParameter("ps");
		if (!StringUtils.isBlank(ps) && ps.matches("[\\d]*")) {
			pagesize = Integer.parseInt(ps);
		}
		return pagesize;
	}

	public int get50Ps(HttpServletRequest request) {
		int pagesize = PAGE_SIZE_FV;
		String ps = request.getParameter("ps");
		if (!StringUtils.isBlank(ps) && ps.matches("[\\d]*")) {
			pagesize = Integer.parseInt(ps);
		}
		return pagesize;
	}

	public String getOrgi(HttpServletRequest request) {
		return getUser(request).getOrgi();
	}
}
